package com.dobby.pea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.GoodDto;
import com.dobby.pea.entity.*;
import com.dobby.pea.mapper.GoodMapper;
import com.dobby.pea.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.border.TitledBorder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Dobby
 * @version 1.0
 */

@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, tbGood> implements GoodService {

    @Autowired
    private GoodImgService goodImgService;

    @Autowired
    private TypeSecondService typeSecondService;

    @Autowired
    private GoodSalesService goodSalesService;

    @Transactional
    public void initSales(tbGood good){
        tbGoodSales tbGoodSales = new tbGoodSales();
        tbGoodSales.setGoodId(good.getId());
        tbGoodSales.setSales(0);
        tbGoodSales.setTypeId(good.getTypeId());

        System.out.println("--------------------------"+tbGoodSales.toString());

        goodSalesService.save(tbGoodSales);
    }

    @Transactional
    public R<Page> GoodDtoByUserPage(int page, int pageSize, String name, Long tid, Long uId) {
        //构造分页构造器对象
        Page<tbGood> goodPage = new Page<>(page,pageSize);
        Page<GoodDto> goodDtoPage = new Page<>();

        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,tbGood::getName,name);
        //类型
        queryWrapper.in(tid != null,tbGood::getTypeId,tid);
        queryWrapper.in(tbGood::getCreateUser,uId);
        queryWrapper.orderByDesc(tbGood::getUpdateTime);

        //执行分页查询
        this.page(goodPage,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(goodPage,goodDtoPage,"records");


        List<tbGood> records = goodPage.getRecords();
        List<GoodDto> list = records.stream().map((item) -> {
            GoodDto goodDto = new GoodDto();

            BeanUtils.copyProperties(item,goodDto);

            Long typeId = item.getTypeId();
            //根据id查询分类对象
            tbTypeSecond typeSecond = typeSecondService.getById(typeId);

            Long id = item.getId();

            LambdaQueryWrapper<tbGoodImg> queryWrappertbGoodImg = new LambdaQueryWrapper<>();
            queryWrappertbGoodImg.in(tbGoodImg::getGoodId,id);

            //该商品的图片集
            List<tbGoodImg> tbGoodImgList = goodImgService.list(queryWrappertbGoodImg);

            goodDto.setImgs(tbGoodImgList);

            if(typeSecond != null){
                String typeName = typeSecond.getName();
                goodDto.setTypeSecond(typeName);
            }

            //商品的销量查询
            tbGoodSales goodSales = goodSalesService.GoodBySales(item.getId());

            if (goodSales!=null){
                goodDto.setSales(goodSales.getSales());
            }else{
                goodDto.setSales(0);
            }


            return goodDto;
        }).collect(Collectors.toList());

        goodDtoPage.setRecords(list);

        return R.success(goodDtoPage);
    }

    @Transactional
    public R<Map<tbType, tbGood>> HotGoodByType(List<Long> ids) {
        HashMap<tbType, tbGood> tbTypetbGoodHashMap = new HashMap<>();
        for (Long id : ids) {
//           goodSalesService.


        }
            return null;
    }


    @Transactional
    public R<Page> GoodDtoPage(int page, int pageSize, String name,Long tid) {
        //构造分页构造器对象
        Page<tbGood> goodPage = new Page<>(page,pageSize);
        Page<GoodDto> goodDtoPage = new Page<>();

        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,tbGood::getName,name);
        //类型
        queryWrapper.in(tid != null,tbGood::getTypeId,tid);
        queryWrapper.orderByDesc(tbGood::getUpdateTime);

        //执行分页查询
        this.page(goodPage,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(goodPage,goodDtoPage,"records");


        List<tbGood> records = goodPage.getRecords();
        List<GoodDto> list = records.stream().map((item) -> {
            GoodDto goodDto = new GoodDto();

            BeanUtils.copyProperties(item,goodDto);

            Long typeId = item.getTypeId();
            //根据id查询分类对象
            tbTypeSecond typeSecond = typeSecondService.getById(typeId);

            Long id = item.getId();

            LambdaQueryWrapper<tbGoodImg> queryWrappertbGoodImg = new LambdaQueryWrapper<>();
            queryWrappertbGoodImg.in(tbGoodImg::getGoodId,id);

            //该商品的图片集
            List<tbGoodImg> tbGoodImgList = goodImgService.list(queryWrappertbGoodImg);

            goodDto.setImgs(tbGoodImgList);

            if(typeSecond != null){
                String typeName = typeSecond.getName();
                goodDto.setTypeSecond(typeName);
            }

            //商品的销量查询
            tbGoodSales goodSales = goodSalesService.GoodBySales(item.getId());

            if (goodSales!=null){
                goodDto.setSales(goodSales.getSales());
            }else{
                goodDto.setSales(0);
            }


            return goodDto;
        }).collect(Collectors.toList());

        goodDtoPage.setRecords(list);

        return R.success(goodDtoPage);

    }

    @Transactional
    public R saveImg(List<tbGoodImg> goodImg) {

        goodImgService.saveBatch(goodImg);

        return R.success("商品图片添加成功");
    }

    @Transactional
    public R getGoodById(Long id) {

        tbGood good = this.getById(id);
        GoodDto goodDto = new GoodDto();
        BeanUtils.copyProperties(good,goodDto);

        Long typeId = good.getTypeId();
        tbTypeSecond typeSecond = typeSecondService.getById(typeId);
        goodDto.setTypeSecond(typeSecond.getName());


        LambdaQueryWrapper<tbGoodImg> queryWrappertbGoodImg = new LambdaQueryWrapper<>();
        queryWrappertbGoodImg.in(tbGoodImg::getGoodId,id);
        //该商品的图片集
        List<tbGoodImg> tbGoodImgList = goodImgService.list(queryWrappertbGoodImg);
        goodDto.setImgs(tbGoodImgList);

        //该商品的销量
        tbGoodSales goodSales = goodSalesService.GoodBySales(id);
        if (goodSales!=null) goodDto.setSales(goodSales.getSales());

        return R.success(goodDto);



    }

    @Transactional
    public R<String> deleteGood(List<Long> ids) {

        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(tbGood::getId,ids);
        int count = this.count(queryWrapper);
        if (count<=0) return R.error("未找到该商品");
        List<tbGood> list = this.list(queryWrapper);
        for (tbGood good : list) {

            //删除照片的信息
            LambdaQueryWrapper<tbGoodImg> queryWrappertbGoodImg = new LambdaQueryWrapper<>();
            queryWrappertbGoodImg.in(tbGoodImg::getGoodId,good.getId());
            goodImgService.remove(queryWrappertbGoodImg);

            //删除销量的信息
            goodSalesService.SalesDelete(good);

//            this.removeById(good.getId());

        }

        //删除商品的记录
        this.remove(queryWrapper);
        return R.success("商品删除成功");
    }




    @Transactional
    public R<String> soldOutGood(List<Long> ids) {
        LambdaQueryWrapper<tbGood> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(tbGood::getId,ids);

        int count = this.count(queryWrapper);

        if (count<=0) return R.error("未找到该商品");

        List<tbGood> list = this.list(queryWrapper);

        //逻辑删除字段为1
        for (tbGood good : list) {
            good.setIsDeleted(1);
        }
        this.updateBatchById(list);

        return R.success("商品已下架");

    }


}
